package com.booksys.controller;

import com.booksys.mapper.AlbumMapper;
import com.booksys.mapper.BorrowrecordMapper;
import com.booksys.mapper.SubalbumMapper;
import com.booksys.pojo.Album;
import com.booksys.pojo.Borrowrecord;
import com.booksys.pojo.Reader;
import com.booksys.pojo.Subalbum;
import com.booksys.result.ResultUtil;
import com.booksys.util.DateTimeUtil;
import com.booksys.util.RedisUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书管理Controller
 */
@RestController
@RequestMapping("/book")
@CrossOrigin
@Api(tags = "图书管理接口")
public class AlbumController {

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private SubalbumMapper subalbumMapper;

    @Autowired
    private BorrowrecordMapper borrowrecordMapper;

    @Autowired
    private  RedisUtil redisUtil;

    /**
     * 获取书目集合
     * @param title
     * @return
     */
    @ApiOperation(value = "获取所有书籍")
    @RequestMapping(value = "/getAllAlbums", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<List<Album>> getAllAlbums(@ApiParam(value="书籍名称")String title){
        ResultUtil<List<Album>> listResultUtil = new ResultUtil<>();
        List<Album> list = new ArrayList<>();
        if (title==null){
            title = "";
        }
        /*查询缓存*/
        System.out.println("***********************************************************************************");
        System.out.println(redisUtil.getValue("list"));
        if(redisUtil.getValue("list")==null){
            /*查询数据库*/
            list = albumMapper.selectByTitle(title);
            /*存入缓存*/
            redisUtil.setValue("list", list);

        }else{
            /*将缓存放入list*/
            List<Album> redisUtilValue1 = (List<Album>) redisUtil.getValue("list");
            ObjectMapper mapper = new ObjectMapper();
            List<Album> redisUtilValue = mapper.convertValue(redisUtilValue1, new TypeReference<List<Album>>() {});

            /*查询某本书籍*/
            if(title.equals("")){
                list=redisUtilValue;
                /*将list存入map返回*/

            }else{

                //TODO
                for(int i=0;i<redisUtilValue.size();i++){
                    if(redisUtilValue.get(i).getTitle().contains(title)){
                        Album album =  redisUtilValue.get(i);
                        list.add(album);
                    }
                }
            }
        }
        return listResultUtil.success(list);
    }

    /**
     * 添加书籍
     * @param title
     * @param author
     * @param publisher
     * @param publishtime
     * @param descri
     * @return
     */
    @ApiOperation(value = "添加书籍")
    @RequestMapping(value = "/addAlbum", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Album> addAlbum(@RequestParam("title")@ApiParam(value="书籍名称")String title,
                                       @RequestParam("author")@ApiParam(value="书籍作者")String author,
                                       @RequestParam("publisher")@ApiParam(value="书籍出版社")String publisher,
                                       @RequestParam("publishtime")@ApiParam(value="书籍发布时间")String publishtime,
                                       @RequestParam("descri")@ApiParam(value="书籍描述")String descri){
        ResultUtil<Album> result = new ResultUtil<>();
        albumMapper.insert(new Album(title,author,publisher,publishtime,0,descri, DateTimeUtil.getDate()));
        redisUtil.delValue("list");

        return result.success();
    }

    /**
     * 添加图书编号
     * @param aid
     * @param number
     * @return
     */
    @ApiOperation(value = "添加书籍副本")
    @RequestMapping(value = "/addSubAlbum", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Subalbum> addSubAlbim(@RequestParam("aid")@ApiParam(value="书籍ID")int aid,
                                            @RequestParam("number")@ApiParam(value="本书编号")String number){
        ResultUtil<Subalbum> result = new ResultUtil<>();
        if(subalbumMapper.selectByNumber(number)!=null){
            return result.fail();
        }
        else{
            Album album=albumMapper.selectById(aid);
            album.setNum(album.getNum()+1);
            albumMapper.updateByPrimaryKey(album);
            subalbumMapper.insert(new Subalbum(aid,number,0,DateTimeUtil.getDate()));

            return result.success();
        }
    }

    /**
     * 借阅图书
     * @param aid
     * @param rid
     * @param raccount
     * @return
     */
    @ApiOperation(value = "借阅书籍")
    @RequestMapping(value = "/borrow", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Subalbum> borrow(@RequestParam("aid")@ApiParam(value="书籍ID")int aid,
                                     @RequestParam("rid")@ApiParam(value="图书剩余数量")int rid,
                                     @RequestParam("raccount")@ApiParam(value="读者用户名")String raccount){
        ResultUtil<Subalbum> result = new ResultUtil<>();
        if(rid!=0){
            Album album=albumMapper.selectById(aid);
            int count=0;
            //找到可借的那本书编号id
            int enableborrowSAid=0;
            int len = album.getSubalbums().size();
            for(int i=0;i<len;i++){
                if(album.getSubalbums().get(i).getCondi()==0){
                    enableborrowSAid=album.getSubalbums().get(i).getSid();
                    count++;
                }
            }
            if(count!=0){
                Borrowrecord borrowrecord=new Borrowrecord(rid,raccount,aid,enableborrowSAid,DateTimeUtil.getDate(),DateTimeUtil.getDateAfter15(),DateTimeUtil.getDateNumber());
                borrowrecordMapper.insert(borrowrecord);
                Subalbum subalbum=subalbumMapper.selectById(enableborrowSAid);
                subalbum.setCondi(1);
                subalbumMapper.updateByPrimaryKey(subalbum);

                redisUtil.delValue("list");
                return result.success();
            }
        }
        return result.fail();
    }

    /**
     * 删除图书
     * @param aid
     * @return
     */
    @ApiOperation(value = "删除书籍")
    @RequestMapping(value = "/deleteAlbum", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Album> deleteAlbum(@RequestParam("aid")@ApiParam(value="书籍ID")int aid){
        ResultUtil<Album> result = new ResultUtil<>();
        Album album = albumMapper.selectById(aid);
        if(album==null){
            return result.fail(album);
        }
        try{
            albumMapper.deleteById(aid);
        }catch (Exception e){
            e.printStackTrace();
        }
        redisUtil.delValue("list");
        return result.success(album);
    }

}
